const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes} = require("../../models/db");

router.post('/deliver/getLngLat', async ctx => {
    let tenantId = ctx.user.tenantId;
    let strIds = await tool.relCompany(tenantId);
    if (strIds.length === 0) {
        return ctx.err('还没有关联配送公司');
    }
    strIds = strIds.join(',');
    let center = [];
    // let lg = ["103.91821831597", "30.619512261285"]
    let options = await db.query(`select * from options where name='lnglat' and tenantId=${tenantId} `, {type: QueryTypes.SELECT, plain: true})
    if (options) {
        let lnglat = options.value;
        center = lnglat.split(',');
    }
    let list = `select a.id,concat(a.name,',',isnull(a.address,'')) as title,a.latitude,a.longitude,a.linkMobile, 
                         b.name as tenantName,a.tenantId from contact as a inner join tenant as b on a.tenantId=b.id  
         where a.latitude is not null and a.longitude is not null and a.tenantId in(${strIds})`
    list = await db.query(list, {type: QueryTypes.SELECT})
    let ret = {
        center: center,
        list: list
    }
    return ctx.suc('', ret);
})

module.exports = router;
